class Sensor

attr_reader :name, :value, :minRaw, :maxRaw, :minEU, :maxEU, :unit, :simulate, :readInterval, :writeInterval
attr_writer :minRaw, :maxRaw, :minEU, :maxEU, :unit, :simulate, :readInterval, :writeInterval

	
	
	@@sensorThreads = []
	@@sensorList = []
	def initialize(name="DEFAULT",minRaw=0,maxRaw=100,minEU=0,maxEU=100,unit="",readInterval=0.1,writeInterval=1)
		@@className = self.class.to_s.upcase+": "
		@@className = @@className.ljust(10)
		@readInterval = readInterval
		@writeInterval = writeInterval
		@simulate = false
		@value = 0
		if name=="DEFAULT"
			name = "SENSOR_"
			name << (@@sensorList.length+1).to_s
		end
		@name = name
		@minRaw = minRaw
		@maxRaw = maxRaw
		@minEU = minEU
		@maxEU = maxEU
		@unit = unit
		
		createString = "NEW: [#{@name}]"
		createString = createString.ljust(25)
		createString << "["
		createString << @minRaw.to_s.rjust(6)
		createString << " | ".ljust(2)
		createString << @maxRaw.to_s.rjust(6)
		createString << "]\t"
		createString << "["
		createString << @minEU.to_s.rjust(6)
		createString << " | "
		createString << @maxEU.to_s.rjust(6)
		createString << "]\t"
		createString << "["
		createString << "#{@unit}]".rjust(10)
		createString << "["
		createString << "#{@readInterval}".rjust(3)
		createString << " | "
		createString << "#{@writeInterval}]".rjust(3)
		
		msg createString
		@@sensorList << self
		@readerThread = Thread.new {readValue}
		@@sensorThreads << @readerThread
		#@readerThread.join
		
	end
	
	def msg(message)
		puts @@className+message
	end
	def Sensor.msg(message)
		puts @@className+message
	end
	
	def getName
		puts @name
	end
	def Sensor.getList
		msg "Current sensors:"
		for aSensor in @@sensorList
			msg "   "+aSensor.name
		end
	end
	def readValue	# reads from sensor
		while true
			sleep @readInterval
			# somehow get a value
			if @simulate
				@value = @value+1
				if @value>@maxEU
					@value=@minEU
				end
				#@value = rand(@maxEU-@minEU)+@minEU
			end
			#value = @value
			#puts @name.upcase+" READING=#{value}"
		end
	end
end